home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / sprites.lzh / Sprites / Example3.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  11KB  |  364 lines

  1. /* Example3                                                          */
  2. /* This program shows how to set up a 15 coloured sprite, and how to */
  3. /* move it around.                                                   */
  4.  
  5.  
  6.  
  7. #include <intuition/intuition.h>
  8. /* Include this file since you are using sprites: */
  9. #include <graphics/sprite.h>
  10.  
  11.  
  12.  
  13. /* Declare the functions we are going to use: */
  14. void main();
  15. void free_memory();
  16.  
  17.  
  18.  
  19. struct IntuitionBase *IntuitionBase = NULL;
  20. /* We need to open the Graphics library since we are using sprites: */
  21. struct GfxBase *GfxBase = NULL;
  22.  
  23.  
  24.  
  25. /* Declare a pointer to a Window structure: */ 
  26. struct Window *my_window = NULL;
  27.  
  28. /* Declare and initialize your NewWindow structure: */
  29. struct NewWindow my_new_window=
  30. {
  31.   50,            /* LeftEdge    x position of the window. */
  32.   25,            /* TopEdge     y positio of the window. */
  33.   320,           /* Width       320 pixels wide. */
  34.   100,           /* Height      100 lines high. */
  35.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  36.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  37.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  38.   RAWKEY,        /*             user has selected the Close window gad, */
  39.                  /*             or if the user has pressed a key. */
  40.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  41.   WINDOWCLOSE|   /*             Close Gadget. */
  42.   WINDOWDRAG|    /*             Drag gadget. */
  43.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  44.   WINDOWSIZING|  /*             Sizing Gadget. */
  45.   ACTIVATE,      /*             The window should be Active when opened. */
  46.   NULL,          /* FirstGadget No Custom gadgets. */
  47.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  48.   "15-COLOURED SPRITE",/* Title Title of the window. */
  49.   NULL,          /* Screen      Connected to the Workbench Screen. */
  50.   NULL,          /* BitMap      No Custom BitMap. */
  51.   80,            /* MinWidth    We will not allow the window to become */
  52.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  53.   300,           /* MaxWidth    than 300 x 200. */
  54.   200,           /* MaxHeight */
  55.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  56. };
  57.  
  58.  
  59.  
  60. /********************************************************/
  61. /* 1. Declare and initialize some sprite graphics data: */
  62. /********************************************************/
  63.  
  64. /* Sprite Data for the Bottom Sprite: */
  65. UWORD chip bottom_sprite_data[36]=
  66. {
  67.   0x0000, 0x0000,
  68.  
  69.   /* Bitplane */
  70.   /* ZERO ONE */
  71.   
  72.   0x0000, 0x0000,
  73.   0xFFFF, 0x0000,
  74.   0x0000, 0xFFFF,
  75.   0xFFFF, 0xFFFF,
  76.  
  77.   0x0000, 0x0000,
  78.   0xFFFF, 0x0000,
  79.   0x0000, 0xFFFF,
  80.   0xFFFF, 0xFFFF,
  81.  
  82.   0x0000, 0x0000,
  83.   0xFFFF, 0x0000,
  84.   0x0000, 0xFFFF,
  85.   0xFFFF, 0xFFFF,
  86.  
  87.   0x0000, 0x0000,
  88.   0xFFFF, 0x0000,
  89.   0x0000, 0xFFFF,
  90.   0xFFFF, 0xFFFF,
  91.     
  92.   0x0000, 0x0000
  93. };
  94.  
  95. /* Sprite Data for the Top Sprite: */
  96. UWORD chip top_sprite_data[36]=
  97. {
  98.   0x0000, SPRITE_ATTACHED, /* We attach the Top Sprite to the Bottom */
  99.                            /* Sprite.                                */
  100.  
  101.   /*  Bitplane  */
  102.   /* TWO  THREE */
  103.   0x0000, 0x0000,
  104.   0x0000, 0x0000,
  105.   0x0000, 0x0000,
  106.   0x0000, 0x0000,
  107.  
  108.   0xFFFF, 0x0000,
  109.   0xFFFF, 0x0000,
  110.   0xFFFF, 0x0000,
  111.   0xFFFF, 0x0000,
  112.  
  113.   0x0000, 0xFFFF,
  114.   0x0000, 0xFFFF,
  115.   0x0000, 0xFFFF,
  116.   0x0000, 0xFFFF,
  117.  
  118.   0xFFFF, 0xFFFF,
  119.   0xFFFF, 0xFFFF,
  120.   0xFFFF, 0xFFFF,
  121.   0xFFFF, 0xFFFF,
  122.     
  123.   0x0000, 0x0000
  124. };
  125.  
  126.  
  127.  
  128. /*********************************************************/
  129. /* 2. Declare and initialize two SimpleSprite structure: */
  130. /*********************************************************/
  131.  
  132. /* Bottom sprite: */
  133. struct SimpleSprite bottom_sprite=
  134. {
  135.   bottom_sprite_data, /* posctldata, pointer to the sprite data. */
  136.   16,                 /* height, 16 lines tall. */
  137.   40, 80,             /* x, y, position on the screen. */
  138.   -1,                 /* num, this field is automatically initialized */
  139.                       /* when you call the GetSprite() function, so   */
  140.                       /* we set it to -1 for the moment.              */
  141. };
  142.  
  143. /* Top sprite: */
  144. struct SimpleSprite top_sprite=
  145. {
  146.   top_sprite_data, /* posctldata, pointer to the sprite data. */
  147.   16,              /* height, 16 lines tall. */
  148.   40, 80,          /* x, y, position on the screen. */
  149.   -1,              /* num, this field is automatically initialized */
  150.                    /* when you call the GetSprite() function, so   */
  151.                    /* we set it to -1 for the moment.              */
  152. };
  153.  
  154.  
  155.  
  156. void main()
  157. {
  158.   /* Sprite position: (We use only one pair of coordinates since the */
  159.   /* two sprites will be attached to each other.)                    */
  160.   WORD x = bottom_sprite.x;
  161.   WORD y = bottom_sprite.y;
  162.  
  163.   /* Direction of the sprite: */
  164.   WORD x_direction = 0;
  165.   WORD y_direction = 0;
  166.  
  167.   /* Boolean variable used for the while loop: */
  168.   BOOL close_me = FALSE;
  169.  
  170.   ULONG class; /* IDCMP */
  171.   USHORT code; /* Code */
  172.  
  173.   /* Declare a pointer to an IntuiMessage structure: */
  174.   struct IntuiMessage *my_message;
  175.  
  176.  
  177.  
  178.   /* Open the Intuition Library: */
  179.   IntuitionBase = (struct IntuitionBase *)
  180.     OpenLibrary( "intuition.library", 0 );
  181.   
  182.   if( IntuitionBase == NULL )
  183.     free_memory("Could NOT open the Intuition Library!");
  184.  
  185.  
  186.  
  187.   /* Since we are using sprites we need to open the Graphics Library: */
  188.   /* Open the Graphics Library: */
  189.   GfxBase = (struct GfxBase *)
  190.     OpenLibrary( "graphics.library", 0);
  191.  
  192.   if( GfxBase == NULL )
  193.     free_memory("Could NOT open the Graphics Library!");
  194.  
  195.  
  196.  
  197.   /* We will now try to open the window: */
  198.   my_window = (struct Window *) OpenWindow( &my_new_window );
  199.   
  200.   /* Have we opened the window succesfully? */
  201.   if(my_window == NULL)
  202.     free_memory("Could NOT open the Window!");
  203.  
  204.  
  205.  
  206.   /* Change the colour register 17 - 31: */
  207.   /* NOTE! Since we change colour register 17, 18 and 19 we will */
  208.   /* change the colour of Intuition's Pointer (Sprite 0). We do  */
  209.   /* not bother about that in this Example but you should be     */
  210.   /* careful with these three colour registers.                  */
  211.   SetRGB4( &my_window->WScreen->ViewPort, 17, 0x0, 0xF, 0x0 );
  212.   SetRGB4( &my_window->WScreen->ViewPort, 18, 0x0, 0xD, 0x0 );
  213.   SetRGB4( &my_window->WScreen->ViewPort, 19, 0x0, 0xB, 0x0 );
  214.   SetRGB4( &my_window->WScreen->ViewPort, 20, 0x0, 0x9, 0x0 );
  215.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0x0, 0x7, 0x1 );
  216.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0x0, 0x5, 0x3 );
  217.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0x3, 0x5 );
  218.   SetRGB4( &my_window->WScreen->ViewPort, 24, 0x1, 0x1, 0x7 );
  219.   SetRGB4( &my_window->WScreen->ViewPort, 25, 0x3, 0x0, 0x5 );
  220.   SetRGB4( &my_window->WScreen->ViewPort, 26, 0x5, 0x0, 0x3 );
  221.   SetRGB4( &my_window->WScreen->ViewPort, 27, 0x7, 0x0, 0x1 );
  222.   SetRGB4( &my_window->WScreen->ViewPort, 28, 0x9, 0x0, 0x0 );
  223.   SetRGB4( &my_window->WScreen->ViewPort, 29, 0xB, 0x0, 0x0 );
  224.   SetRGB4( &my_window->WScreen->ViewPort, 30, 0xD, 0x0, 0x0 );
  225.   SetRGB4( &my_window->WScreen->ViewPort, 31, 0xF, 0x0, 0x0 );
  226.  
  227.  
  228.  
  229.   /* Reserve sprite 2 as Bottom Sprite: */
  230.   if( GetSprite( &bottom_sprite, 2 ) != 2 )
  231.     free_memory("Could NOT reserve Hardware Sprite 2!"); /* Error! */
  232.  
  233.   /* Reserve sprite 3 as Top Sprite: */
  234.   if( GetSprite( &top_sprite, 3 ) != 3 )
  235.     free_memory("Could NOT reserve Hardware Sprite 3!"); /* Error! */
  236.  
  237.  
  238.  
  239.   /* We will now move the two sprites so that we can see them: */
  240.   /* (After you have reserved a sprite you need to call either */
  241.   /* MoveSprite() or ChangeSprite() inorder to display the     */
  242.   /* sprite.)                                                  */
  243.   MoveSprite( 0, &bottom_sprite, x, y );
  244.   MoveSprite( 0, &top_sprite, x, y );
  245.  
  246.  
  247.  
  248.   /* Stay in the while loop until the user has selected the Close window */
  249.   /* gadget: */
  250.   while( close_me == FALSE )
  251.   {
  252.     /* Stay in the while loop as long as we can collect messages */
  253.     /* sucessfully: */
  254.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  255.     {
  256.       /* After we have collected the message we can read it, and save any */
  257.       /* important values which we maybe want to check later: */
  258.       class = my_message->Class;
  259.       code  = my_message->Code;
  260.  
  261.  
  262.       /* After we have read it we reply as fast as possible: */
  263.       /* REMEMBER! Do never try to read a message after you have replied! */
  264.       /* Some other process has maybe changed it. */
  265.       ReplyMsg( my_message );
  266.  
  267.  
  268.       /* Check which IDCMP flag was sent: */
  269.       switch( class )
  270.       {
  271.         case CLOSEWINDOW:     /* Quit! */
  272.                close_me=TRUE;
  273.                break;  
  274.  
  275.         case RAWKEY:          /* A key was pressed! */
  276.                /* Check which key was pressed: */
  277.                switch( code )
  278.                {
  279.                  /* Up Arrow: */
  280.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  281.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  282.  
  283.                  /* Down Arrow: */
  284.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  285.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  286.  
  287.                  /* Right Arrow: */
  288.                  case 0x4E:      x_direction = 1; break; /* Pressed */
  289.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  290.  
  291.                  /* Left Arrow: */
  292.                  case 0x4F:      x_direction = -1; break; /* Pressed */
  293.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  294.                }
  295.                break;  
  296.       }
  297.     }
  298.  
  299.  
  300.  
  301.     /* Change the x/y position: */
  302.     x += x_direction;
  303.     y += y_direction;
  304.     
  305.     /* Check that the sprite does not move outside the screen: */
  306.     if(x > 320)
  307.       x = 320;
  308.     if(x < 0)
  309.       x = 0;
  310.     if(y > 200)
  311.       y = 200;
  312.     if(y < 0)
  313.       y = 0;
  314.  
  315.  
  316.  
  317.     /* Move the bottom sprite: */
  318.     /* IMPORTANT! If you move the Bottom Sprite the Top Sprite will      */
  319.     /* automatically be moved too. However, if you move the Top Sprite   */
  320.     /* the Bottom Sprite will not be moved, and the Attach function will */
  321.     /* not work any more. (You then get two 3-coloured sprites.)         */
  322.     MoveSprite( 0, &bottom_sprite, x, y );
  323.  
  324.  
  325.  
  326.     /* Wait for the videobeam to reach the top of the display: (This */
  327.     /* will slow down the animation so the user can see the sprite)  */
  328.     /* (If you want to have some "action" you can take it away...)   */
  329.     WaitTOF();
  330.   }
  331.  
  332.  
  333.     
  334.   /* Free all allocated memory: (Close the window, libraries etc) */
  335.   free_memory("THE END");
  336.  
  337.   /* THE END */
  338. }
  339.  
  340.  
  341.  
  342. /* This function frees all allocated memory. */
  343. void free_memory( message )
  344. STRPTR message;
  345. {
  346.   printf( "%s\n", message );
  347.   
  348.   if( bottom_sprite.num != -1 )
  349.     FreeSprite( bottom_sprite.num );
  350.  
  351.   if( top_sprite.num != -1 )
  352.     FreeSprite( top_sprite.num );
  353.  
  354.   if( my_window )
  355.     CloseWindow( my_window );
  356.   
  357.   if( GfxBase )
  358.     CloseLibrary( GfxBase );
  359.  
  360.   if( IntuitionBase )
  361.     CloseLibrary( IntuitionBase );
  362.  
  363.   exit();
  364. }